<?php

namespace App\Models\Dev;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use App\Models\Common\CommonModel;
class CoalArea extends CommonModel
{
    use HasFactory;

    protected $table = 'coal_area';
    protected $appends = ['level_name'];

    public function getParent(){
        return $this->hasOne(CoalArea::class,'pid','id');
    }

    public function getBelong(){
        return $this->hasOne(CoalArea::class,'id','pid');
    }

    public function moreChildren() {
        return $this->hasMany(CoalArea::class,'pid','id')->orderBy('id');
    }

    public function children() {
        return $this->moreChildren()->with(['children']);
    }


    public function getLevelNameAttribute(){
        $map = [
            1=>'矿',
            2=>'区域',
            3=>'地点',
        ];

        return $map[$this->level]??'未知';
    }

    public function getLst(array $conditions = []){
        $records = parent::getLst($conditions); // TODO: Change the autogenerated stub
        if(!empty($conditions['level'])){
            $records = $records->where('level',$conditions['level']);
        }

        return $records;
    }
}
